/**
 * Calculate the data that's needed to draw the image in the image editor on the
 * resulting image. E.g. remove left part of the image, resize the image, move the image, ...
 */
function calculateDrawData(args) {
	//left offset of the image element
	var imageLeft = args[0];
	//width of the image element
	var imageWidth = args[1];
	//top offset of the image element
	var imageTop = args[2];
	//height of the image element
	var imageHeight = args[3];
	var imageRight = imageLeft + imageWidth;
	var imageBottom = imageTop + imageHeight;

	//left offset of the container element
	var containerLeft = args[4];
	//width of the container element
	var containerWidth = args[5];
	//top offset of the container element
	var containerTop = args[6];
	//height of the container element
	var containerHeight = args[7];
	var containerRight = containerLeft + containerWidth;
	var containerBottom = containerTop + containerHeight;

	//the effective width of the given image
	var realImageWidth = args[8];
	//the effective height of the given image
	var realImageHeight = args[9];
	
	var canvasWidthInch = args[10];
	var canvasHeightInch = args[11];
	

	sourceX = -1;
	sourceY = -1;
	sourceWidth = -1;
	sourceHeight = -1;
	destX = -1;
	destY = -1;
	destWidth = -1;
	destHeight = -1;
	dpi_x = -1;
	dpi_y = -1;

	if (overlaps(imageWidth, containerWidth, imageHeight, containerHeight, imageLeft, containerLeft, imageTop, containerTop)) {
		//image boundaries are the same as the ones from the canvas
		sourceX = imageLeft;
		sourceY = imageTop;
		sourceWidth = imageWidth;
		sourceHeight = imageHeight;
		destX = 0;
		destY = 0;
		destWidth = imageWidth;
		destHeight = imageHeight;
	} else if (imageOutsideCanvas(imageLeft, imageRight, imageTop, imageBottom, containerLeft, containerRight, containerTop, containerBottom)) {
		sourceX = 0;
		sourceY = 0;
		sourceWidth = 0;
		sourceHeight = 0;
		destX = 0;
		destY = 0;
		destWidth = 0;
		destHeight = 0;
	} else {
			if (imageOutsideCanvas(imageLeft, imageRight, imageTop, imageBottom, containerLeft, containerRight, containerTop, containerBottom)) {
				sourceX = 1; //TODO remove
			} else if(imageInCanvasForYDirection(imageTop, imageBottom, containerTop, containerBottom) &&
					imageInCanvasForXDirection(imageLeft, imageRight, containerLeft, containerRight)){
				sourceX = 0;
				sourceY = 0;
				sourceWidth = realImageWidth;
				sourceHeight = realImageHeight;
				destX = getMaximum(imageLeft, containerLeft) - containerLeft;
				destY = getMaximum(imageTop, containerTop) - containerTop;
				destWidth = imageWidth;
				destHeight = imageHeight;
			
			} else {
				tempSourceX = ((imageLeft < containerLeft) ? containerLeft - imageLeft : 0);
				tempSourceY = ((imageTop < containerTop) ? containerTop - imageTop : 0);
				tempEndY = getMinimum(imageBottom, containerBottom) - getMinimum(imageTop, containerTop);
				xCorrection = (((imageRight > containerRight) ? imageRight - containerRight : 0)/imageWidth) * realImageWidth;
				yCorrection = (((imageBottom > containerBottom) ? imageBottom - containerBottom : 0)/imageHeight) * realImageHeight;
				
				sourceX = (tempSourceX / imageWidth) * realImageWidth;
				sourceY = (tempSourceY / imageHeight) * realImageHeight;
				sourceWidth = ((1 - (tempSourceX / imageWidth)) * realImageWidth) - xCorrection;
				sourceHeight = ((1- (tempSourceY / imageHeight)) * realImageHeight) - yCorrection;
				destX = getMaximum(imageLeft, containerLeft) - containerLeft;
				destY = getMaximum(imageTop, containerTop) - containerTop;
				destWidth = getMinimum(imageRight, containerRight) - getMaximum(imageLeft, containerLeft);
				destHeight = getMinimum(imageBottom, containerBottom) - getMaximum(imageTop, containerTop);
			}
		

		sourceX = Math.round(sourceX * 1000000000000) / 1000000000000;
		sourceY = Math.round(sourceY * 1000000000000) / 1000000000000;
		sourceWidth = Math.round(sourceWidth * 1000000000000) / 1000000000000;
		sourceHeight = Math.round(sourceHeight * 1000000000000) / 1000000000000;
		destX = Math.round(destX * 1000000000000) / 1000000000000;
		destY = Math.round(destY * 1000000000000) / 1000000000000;
		destWidth = Math.round(destWidth * 1000000000000) / 1000000000000;
		destHeight = Math.round(destHeight * 1000000000000) / 1000000000000;
	}
	if(destWidth == 0 && destHeight == 0 ){
		dpi_x = 0;
		dpi_y = 0;
	} else {
		dpi_x = ((destWidth / imageWidth) * realImageWidth) / ((destWidth/containerWidth) * (canvasWidthInch));
		dpi_y = ((destHeight / imageHeight) * realImageHeight) / ((destHeight/containerHeight) * (canvasHeightInch));
		dpi_x = Math.round(dpi_x * 1000000000000) / 1000000000000;
		dpi_y = Math.round(dpi_y * 1000000000000) / 1000000000000;
	}
	
	return [ sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, dpi_x, dpi_y];
}

function imageInCanvasForXDirection(imageLeft, imageRight, containerLeft, containerRight) {
	return (imageLeft > containerLeft) && (imageRight < containerRight);
}

function imageInCanvasForYDirection(imageTop, imageBottom, containerTop, containerBottom) {
	return (imageTop > containerTop) && (imageBottom < containerBottom);
}

function imageOutsideCanvas(imageLeft, imageRight, imageTop, imageBottom, containerLeft, containerRight, containerTop, containerBottom) {
	return (imageLeft > containerRight) || (imageRight < containerLeft) || (imageBottom < containerTop) || (imageTop > containerBottom);
}

function hasSameWidth(object1Width, object2Width) {
	return object1Width == object2Width;
}

function hasSameHeight(object1Height, object2Height) {
	return object1Height == object2Height;
}

function imageResized(oldWidth, oldHeight, newWidth, newHeight) {
	return !(hasSameWidth(oldWidth, newWidth) && hasSameHeight(oldHeight, newHeight));
}

function overlaps(object1Width, object2Width, object1Height, object2Height, object1Left, object2Left, object1Top, object2Top) {
	var notResized = !imageResized(object1Width, object2Height, object2Width, object2Height);
	return notResized && (object1Left == object2Left) && (object1Top == object2Top);
}

function getMinimum(value1, value2) {
	if (value1 < value2) {
		return value1;
	} else {
		return value2;
	}
}

function getMaximum(value1, value2) {
	if (value1 < value2) {
		return value2;
	} else {
		return value1;
	}
}